<template>
  <div class="export-excel-container">
    <div style="display: flex">
      <div>
        <label class="radio-label" style="padding-left: 0">文件名：</label>
        <el-input
          v-model="filename"
          placeholder="请输入导出的文件名，默认为 excel-table"
          style="width: 345px"
          :prefix-icon="Document"
        />
      </div>

      <div>
        <label class="radio-label">Cell Auto-Width：</label>
        <el-radio-group v-model="autoWidth">
          <el-radio :value="true" border>True</el-radio>
          <el-radio :value="false" border>False</el-radio>
        </el-radio-group>
      </div>

      <div>
        <label class="radio-label">文件类型：</label>
        <el-select v-model="bookType" style="width: 120px">
          <el-option v-for="item in options" :key="item" :label="item" :value="item" />
        </el-select>
      </div>

      <el-button
        v-waves
        plain
        :loading="downloadLoading"
        style="margin: 0 0 20px 20px"
        type="success"
        :icon="Top"
        @click="handleDownload"
      >
        Excel Export
      </el-button>
    </div>

    <el-table :data="tableData" border highlight-current-row row-key="id" style="width: 100%" ref="table">
      <el-table-column prop="id" label="ID" width="70" align="center" sortable></el-table-column>
      <el-table-column prop="date" label="日期" width="180px"></el-table-column>
      <el-table-column prop="name" label="姓名" width="150px"></el-table-column>
      <el-table-column prop="title" min-width="100" label="标题"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column prop="status" label="状态" width="140">
        <template #default="{ row }">
          <el-tag :type="tableStatusFilter(row.status)">
            {{ row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="priority" label="优先级" width="140">
        <template #default="{ row }">
          <Icon v-for="n in row.priority" :key="n" name="star" style="color: #606266" />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup lang="ts" name="ExportExcel">
import { exportJsonToExcel, formatJsonToArray } from "@/utils";
import { largeData } from "@/mock/table";
import { tableStatusFilter } from "@/config/constant";
import { Document, Top } from "@element-plus/icons-vue";

const options = ["xlsx", "csv", "txt"];

const tableData = ref(largeData);
const downloadLoading = ref(false);
const filename = ref("");
const autoWidth = ref(true);
const bookType = ref("xlsx");

const handleDownload = () => {
  downloadLoading.value = true;
  const tHeader = ["ID", "Name", "Date", "Address", "Status", "Priority", "Title"];
  const filterVal = ["id", "name", "date", "address", "status", "priority", "title"];
  const list = tableData.value;
  const data = formatJsonToArray(list, filterVal);
  exportJsonToExcel(
    tHeader,
    data,
    filename.value !== "" ? filename.value : undefined,
    undefined,
    undefined,
    autoWidth.value,
    bookType.value
  );
  downloadLoading.value = false;
};
</script>

<style lang="scss" scoped>
.export-excel-container {
  padding: 10px 12px;
  background-color: #ffffff;

  .radio-label {
    padding: 0 12px 0 30px;
    font-size: 14px;
    color: #606266;
  }
}
</style>
